package Single;
//单例模式-懒汉式
//在使用的时候创建，但存在线程安全的问题
public class SingleTon02 {
    public static void main(String[] args) {
        System.out.println(Cat.n1);
        Cat cat = Cat.getInstance();
        System.out.println(cat);

        Cat cat1 = Cat.getInstance();
        System.out.println(cat1);
        System.out.println(cat == cat1);
    }
}

class Cat{
    private String name;
    public static int n1 = 999;

    private static Cat cat;

    private Cat(String name) {
        System.out.println("构造器被调用..");
        this.name = name;
    }

    public static Cat getInstance(){
        if(cat == null){
            cat = new Cat("泡芙");
        }
        return cat;
    }

    @Override
    public String toString() {
        return "Cat{" +
                "name='" + name + '\'' +
                '}';
    }
}
